package com.fan.shop.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseLineId;

@Named("purchaseLineDao")
public class PurchaseLineDaoImpl implements PurchaseLineDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public PurchaseLine getPurchaseLine(String poNumber, String itemCode) {
		Session session = sessionFactory.getCurrentSession();
		PurchaseLineId id = new PurchaseLineId();
		id.setPoNumber(poNumber);
		id.setItemCode(itemCode);
		PurchaseLine purchaseLine = (PurchaseLine) session.get(
				PurchaseLine.class, id);
		return purchaseLine;
	}

	@SuppressWarnings("unchecked")
	public List<PurchaseLine> getAllPurchaseLines() {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(PurchaseLine.class);
		List<PurchaseLine> list = (List<PurchaseLine>) criteria.list();
		return list;
	}

	public void addPurchaseLine(PurchaseLine purchaseLine) {
		Session session = sessionFactory.getCurrentSession();
		session.save(purchaseLine);
	}

	public void updatePurchaseLine(PurchaseLine purchaseLine) {
		Session session = sessionFactory.getCurrentSession();
		session.update(purchaseLine);
	}

	public void deletePurchaseLine(PurchaseLine purchaseLine) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(purchaseLine);
	}

	public boolean hasPurchaseLine(String poNumber, String itemCode) {
		if (getPurchaseLine(poNumber, itemCode) == null) {
			return false;
		} else {
			return true;
		}
	}

	public List<String> getColumnNames() {
		List<String> columns = new ArrayList<String>();
		columns.add("PO Number");
		columns.add("Line No");
		columns.add("Item Code");
		columns.add("Quantity");
		columns.add("Price");
		return columns;
	}

}
